package com.opensky.qatest.ui.page;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.Report;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

public class EmailsPage extends LoadableComponent<EmailsPage> implements IPage {

    // ************************ Locators ************************

    private static final String EMAIL_PAGE_URL = "https://opensky.com/account/subscriptions";

    private static final String ALL_EMAIL_UPDATES = "//input[@id='account_subscription_management_subscription_all' and @checked='checked']";

    // ************************ Complex page objects **************************

    private final WebDriverWrapper driver;

    public EmailsPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(EMAIL_PAGE_URL);
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(Report.wrongURL(EMAIL_PAGE_URL, url), LogicUtils.isURLEqual(EMAIL_PAGE_URL, url));
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(EMAIL_PAGE_URL);
    }

    public boolean isAllEmailUpdatesCHecked() {
	driver.waitAndVerifyElementPresent("ALL_EMAIL_UPDATES", By.xpath(ALL_EMAIL_UPDATES));
	return true;
    }

    // ************************ GET/SET methods **************************

    @Override
    public String getName() {
	return "Emails page";
    }
}